package com.payment.ktb.activity.main1;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.constants.Constants;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.model.MessageNoticeEntity;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.view.BadgeView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by admin on 2017/9/21.
 */

public class MessageListActivity  extends BaseActivity{


    @BindView(R.id.tv_messagelist_systemlastmsg)
    TextView tv_messagelist_systemlastmsg;
    @BindView(R.id.tv_messagelist_sysmtemlastmsgtime)
    TextView tv_messagelist_sysmtemlastmsgtime;
    @BindView(R.id.tv_messagelist_activelastmsg)
    TextView tv_messagelist_activelastmsg;
    @BindView(R.id.tv_messagelist_activelastmsgtime)
    TextView tv_messagelist_activelastmsgtime;
    @BindView(R.id.iv_messagelist_icon1)
    ImageView iv_messagelist_icon1;
    @BindView(R.id.iv_messagelist_icon2)
    ImageView iv_messagelist_icon2;
    BadgeView badgeViewSystem;
    BadgeView badgeViewActive;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messagelist);
        ButterKnife.bind(this);
        initTitle("消息列表");
        badgeViewSystem = new BadgeView(mContext);
        badgeViewActive = new BadgeView(mContext);
        initData();
    }


    private void initData() {

    tv_messagelist_systemlastmsg.setText(getIntent().getStringExtra("systemLastMsg"));
    tv_messagelist_sysmtemlastmsgtime.setText(getIntent().getStringExtra("systemLastMsgTime"));
    tv_messagelist_activelastmsg.setText(getIntent().getStringExtra("activeLastMsg"));
    tv_messagelist_activelastmsgtime.setText(getIntent().getStringExtra("activeLastMsgTime"));


        //设置badgeview
        if (getIntent().getIntExtra("systemUnRead",0) != 0) {
            badgeViewSystem = new BadgeView(mContext);
            badgeViewSystem.setTargetView(iv_messagelist_icon1);
            badgeViewSystem.setBadgeCount(getIntent().getIntExtra("systemUnRead",0));
            badgeViewSystem.setTextColor(getResources().getColor(R.color.white));
            badgeViewSystem.setBackground(10,getResources().getColor(R.color.red));
            badgeViewSystem.setTextSize(8);
        }else {
            badgeViewSystem.setVisibility(View.GONE);
        }

        //设置badgeview
        if (getIntent().getIntExtra("activeUnRead",0) != 0) {
            badgeViewActive = new BadgeView(mContext);
            badgeViewActive.setTargetView(iv_messagelist_icon2);
            badgeViewActive.setBadgeCount(getIntent().getIntExtra("activeUnRead",0));
            badgeViewActive.setTextColor(getResources().getColor(R.color.white));
            badgeViewActive.setBackground(10,getResources().getColor(R.color.red));
            badgeViewActive.setTextSize(8);
        }else {
            badgeViewActive.setVisibility(View.GONE);
        }


    }


    @OnClick({R.id.ll_messagelist_item1,R.id.ll_messagelist_item2})
    public void clickEvent(View view) {

        switch (view.getId()) {


            //系统消息
            case R.id.ll_messagelist_item1:

                getSystemMsg();

                break;

            //活动消息
            case R.id.ll_messagelist_item2:
                getActiveMsg();

                break;
        }

    }


    //获取系统通知
    public void getSystemMsg() {


        progressDialogUtils.showProgress();

        //构造传输参数
        Map<String, String> map = new HashMap<String,String>();
        //1.系统消息，2活动消息
        map.put("noticeType", "1");
        map.put("page","0");

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.UserNoticeMsgListState, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    Intent intent = new Intent(mContext, MessageNoticeActivity.class);
                    intent.putExtra("records",jsonObject.optString("records"));
                    badgeViewSystem.setVisibility(View.GONE);
                    startActivity(intent);


                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });
    }




    //获取活动通知
    public void getActiveMsg() {
        progressDialogUtils.showProgress();

        //构造传输参数
        Map<String, String> map = new HashMap<String,String>();
        //1.系统消息，2活动消息
        map.put("noticeType", "2");
        map.put("page","0");

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.UserNoticeMsgListState, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    Intent intent = new Intent(mContext, ActiveMsgActivity.class);
                    intent.putExtra("records",jsonObject.optString("records"));
                    badgeViewActive.setVisibility(View.GONE);
                    startActivity(intent);


                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });


    }


}
